package ie.dcu.spca.model.dao;

import ie.dcu.spca.model.Bean;
import ie.dcu.spca.model.CommonOperation;
import ie.dcu.spca.model.DBconnection;
import ie.dcu.spca.model.Dao;
import ie.dcu.spca.model.bean.User;
import ie.dcu.spca.model.sql.userSQL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;

public class UserDao extends Dao implements CommonOperation {
    private User cursorToUser(Cursor cursor) {
        cursor.moveToNext();
        int idIndex = cursor.getColumnIndex("id");
        int nameIndex = cursor.getColumnIndex("name");
        int creat_atIndex = cursor.getColumnIndex("create_at");
        int update_atIndex = cursor.getColumnIndex("update_at");
        int delete_atIndex = cursor.getColumnIndex("delete_at");
        int id = cursor.getInt(idIndex);
        String name = cursor.getString(nameIndex);
        long creat_at = cursor.getLong(creat_atIndex);
        long update_at = cursor.getLong(update_atIndex);
        long delete_at = cursor.getLong(delete_atIndex);
        cursor.close();
        return new User(id, name, creat_at, update_at, delete_at);
    }

    private List<User> cursorToUserlist(Cursor cursor) {
        List<User> userlist = new ArrayList<User>();
        while (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("id");
            int nameIndex = cursor.getColumnIndex("name");
            int creat_atIndex = cursor.getColumnIndex("create_at");
            int update_atIndex = cursor.getColumnIndex("update_at");
            int delete_atIndex = cursor.getColumnIndex("delete_at");
            int id = cursor.getInt(idIndex);
            String name = cursor.getString(nameIndex);
            long creat_at = cursor.getLong(creat_atIndex);
            long update_at = cursor.getLong(update_atIndex);
            long delete_at = cursor.getLong(delete_atIndex);
            userlist.add(new User(id, name, creat_at, update_at, delete_at));
        }
        cursor.close();
        return userlist;
    }

    public User selectUserByName(String name) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(userSQL.selectUserByName, name);
        Cursor cursor = conn.querySql(sql);
        User user = this.cursorToUser(cursor);
        return user;
    }

    public int insertUser(String name) {
        DBconnection conn = DBconnection.getInstance();
        int maxid = this.selectMaxId();
        //String sql = String.format(userSQL.insertByName, );
        Object[] args = {maxid + 1, name,
                (new Date()).getTime(), 0, 0};
        conn.executeSql(userSQL.insertByName, args);
        return maxid + 1;
    }

    @Override
    public Bean selectById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(userSQL.selectById, id);
        Cursor cursor = conn.querySql(sql);
        User user = this.cursorToUser(cursor);
        return user;
    }

    @Override
    public int insertByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        int id = ((User) bean).getId();
        if (this.existById(id)) {
            return -1;
        }
        String name = ((User) bean).getName();
        long create_at = ((User) bean).getCreate_at().getTime();
        long update_at = ((User) bean).getUpdate_at().getTime();
        long delete_at = ((User) bean).getDelete_at().getTime();
        Object[] args = { id, name, create_at,
                update_at, delete_at};
        conn.executeSql(userSQL.insertByBean,args);
        return id;
    }

    @Override
    public boolean deleteById(int id) {
        DBconnection conn = DBconnection.getInstance();
        if (!this.isDeletedById(id)) {
            return false;
        }
        Object[] args = { (new Date()).getTime(), id};
        conn.executeSql(userSQL.deleteById, args);
        return true;
    }

    @Override
    public boolean deleteByBean(Bean bean) {
        return this.deleteById(((User) bean).getId());
    }

    @Override
    public boolean updateByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        User user = (User) bean;
        int id = user.getId();
        if (!this.existById(id)) {
            return false;
        }
        String name = user.getName();
        long create_at = user.getCreate_at().getTime();
        long update_at = (new Date()).getTime();
        long delete_at = user.getDelete_at().getTime();
        Object[] args= { name, create_at,
                update_at, delete_at, id};
        conn.executeSql(userSQL.updateByBean,args);
        return true;
    }

    @Override
    public int selectMaxId() {
        DBconnection conn = DBconnection.getInstance();
        String sql = userSQL.selectMaxId;
        Cursor cursor = conn.querySql(sql);
        int maxid = 0;
        if (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("max");
            maxid = cursor.getInt(idIndex);
        }
        cursor.close();
        return maxid;
    }

    @Override
    public boolean existById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(userSQL.existById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public boolean isDeletedById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(userSQL.isDeletedById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public List<Bean> selectAllBean() {
        DBconnection conn = DBconnection.getInstance();
        String sql = userSQL.selectAllBean;
        Cursor cursor = conn.querySql(sql);
        List<User> userlist = this.cursorToUserlist(cursor);
        List<Bean> beanlist = new ArrayList<Bean>();
        for (User b : userlist) {
            beanlist.add(b);
        }
        cursor.close();
        return beanlist;
    }
}
